package com.github.abel533.listener;

import com.github.abel533.C;
import org.springframework.aop.framework.AdvisedSupport;
import org.springframework.aop.framework.AdvisedSupportListener;
import org.springframework.beans.factory.parsing.*;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringApplicationRunListener;
import org.springframework.boot.autoconfigure.AutoConfigurationImportEvent;
import org.springframework.boot.autoconfigure.AutoConfigurationImportListener;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.event.GenericApplicationListener;
import org.springframework.context.event.SmartApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.ResolvableType;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.jmx.export.MBeanExporterListener;
import org.springframework.stereotype.Component;

import javax.management.ObjectName;

/**
 * @author liuzh
 */
@Component
public class ListenerImpl implements SpringApplicationRunListener, AutoConfigurationImportListener,
    ReaderEventListener, AdvisedSupportListener, GenericApplicationListener,
    SmartApplicationListener, MBeanExporterListener {

    public ListenerImpl() {
        C.print("ListenerImpl#constructor");
    }

    public ListenerImpl(SpringApplication springApplication, String[] args) {
        C.print("SpringApplicationRunListener#constructor");
    }

    @Override
    public void starting() {
        C.print("SpringApplicationRunListener#starting");
    }

    @Override
    public void environmentPrepared(ConfigurableEnvironment environment) {
        C.print("SpringApplicationRunListener#environmentPrepared");
    }

    @Override
    public void contextPrepared(ConfigurableApplicationContext context) {
        C.print("SpringApplicationRunListener#contextPrepared");
    }

    @Override
    public void contextLoaded(ConfigurableApplicationContext context) {
        C.print("SpringApplicationRunListener#contextLoaded");
    }

    @Override
    public void started(ConfigurableApplicationContext context) {
        C.print("SpringApplicationRunListener#started");
    }

    @Override
    public void running(ConfigurableApplicationContext context) {
        C.print("SpringApplicationRunListener#running");
    }

    @Override
    public void failed(ConfigurableApplicationContext context, Throwable exception) {
        C.print("SpringApplicationRunListener#failed");
    }

    @Override
    public void onAutoConfigurationImportEvent(AutoConfigurationImportEvent event) {
        C.print("AutoConfigurationImportListener#onAutoConfigurationImportEvent");
    }

    @Override
    public void defaultsRegistered(DefaultsDefinition defaultsDefinition) {
        C.print("ReaderEventListener#defaultsRegistered");
    }

    @Override
    public void componentRegistered(ComponentDefinition componentDefinition) {
        C.print("ReaderEventListener#componentRegistered");
    }

    @Override
    public void aliasRegistered(AliasDefinition aliasDefinition) {
        C.print("ReaderEventListener#aliasRegistered");
    }

    @Override
    public void importProcessed(ImportDefinition importDefinition) {
        C.print("ReaderEventListener#importProcessed");
    }

    @Override
    public void activated(AdvisedSupport advised) {
        C.print("AdvisedSupportListener#activated");
    }

    @Override
    public void adviceChanged(AdvisedSupport advised) {
        C.print("AdvisedSupportListener#adviceChanged");
    }

    @Override
    public boolean supportsEventType(ResolvableType eventType) {
        C.print("GenericApplicationListener#supportsEventType");
        return false;
    }

    @Override
    public boolean supportsSourceType(Class<?> sourceType) {
        C.print("GenericApplicationListener#supportsSourceType");
        return false;
    }

    @Override
    public int getOrder() {
        C.print("GenericApplicationListener#getOrder");
        return Ordered.LOWEST_PRECEDENCE;
    }

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        C.print("ApplicationListener#onApplicationEvent - " + event.getClass().getSimpleName());
    }

    @Override
    public void mbeanRegistered(ObjectName objectName) {
        C.print("MBeanExporterListener#mbeanRegistered");
    }

    @Override
    public void mbeanUnregistered(ObjectName objectName) {
        C.print("MBeanExporterListener#mbeanUnregistered");
    }

    @Override
    public boolean supportsEventType(Class<? extends ApplicationEvent> eventType) {
        C.print("SmartApplicationListener#supportsEventType");
        return false;
    }
}
